function createXHR() 
{
    var request = false;
	try {
		request = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (err2) {
		try {
			request = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (err3) {
			try {
				request = new XMLHttpRequest();
			}
			catch (err1) 
			{
				request = false;
			}
        }
    }
    return request;
}

function Write(url, content)
{
	return Write(url, content, false);
}

function Write(url, content, forcereload)	// url is the script and data is a string of parameters
{ 
	var xhr = createXHR();

	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState  == 4)
		{
			if(xhr.status  == 200) {
				if(forcereload)
					location.reload();
				return xhr.responseText;
			}
			else 
				alert("Error code " + xhr.status);
		}
	}

	xhr.open("POST", url, true);		
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(content); 
} 

function submitVolume(vol)
{ 
	Write("volume.php", "volume=" + vol);
}

function submitAction(action)
{
	Write("action.php", "action=" + action, true);
}

function submitRate(rate)
{
	var xhr = createXHR();

	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState  == 4)
		{
			if(xhr.status  == 200)
				showPopupRate();
			else 
				alert("Error code " + xhr.status);
		}
	}

	xhr.open("POST", "rate.php", true);		
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("rate=" + rate); 
}

function submitRateTrack(form)
{
	var query = "rate=" + form.rate.value;
	for(i = 0; i < form.length; i++) {
		if(form[i].type == 'checkbox' && form[i].checked && form[i].name=="select[]") {
			query += "&select[]=" + form[i].value;
		}
	}
	Write("rate.php", query, true);
}

function showPopupRate()
{
	document.getElementById("popuprate").style.display="block";
	document.getElementById("linksrate").style.display="none";
	setTimeout("hidePopupRate()", 2000);
}

function hidePopupRate()
{
	document.getElementById("linksrate").style.display="block";
	document.getElementById("popuprate").style.display="none";
	location.reload();
}


function showTracks(i)
{
	if(document.getElementById("tracks" + i).style.display == 'block') {
		document.getElementById("tracks" + i).style.display = 'none';
	} else {

		var xhr = createXHR();

		xhr.onreadystatechange=function()
		{ 
			if(xhr.readyState  == 4)
			{
				if(xhr.status  == 200) {
					document.getElementById("tracks" + i).innerHTML = xhr.responseText;
					document.getElementById("tracks" + i).style.display = 'block';
				}
				else 
					alert("Error code " + xhr.status);
			}
		}

		xhr.open("POST", "tracks.php", true);		
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("album=" + i); 
	
	}

	return false;
}


// Checkall to queue several songs at once

function checkall(form, value)
{
	for(i = 0; i < form.length; i++) {
		if(form[i].type == 'checkbox') 
			form[i].checked=value;
	}
}

// Login :

function guest() {
	if(document.login_box.username.value == "" && document.login_box.password.value == "") {
		document.login_box.username.value = "guest";
		document.login_box.password.value = "guest";
	}
}
